package cn.semcorp.zhuhaizj.listener;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.pojo.dto.ClassDataDTO;
import cn.semcorp.zhuhaizj.pojo.dto.CoatingDateDTO;
import cn.semcorp.zhuhaizj.pojo.dto.TrainingDataDTO;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.List;
import java.util.regex.Pattern;


/**
 * 涂布产出数据读取分析器
 */
public class ReadClassDateListener extends AnalysisEventListener<ClassDataDTO> {

    /**
     * 读时aop
     * @param coatingDateDTO
     * @param analysisContext
     */
    public List<ClassDataDTO> classDateDTOList ;
    public ReadClassDateListener(List<ClassDataDTO> list){
        this.classDateDTOList = list;
    }
    @Override
    public void invoke(ClassDataDTO coatingDateDTO, AnalysisContext analysisContext) {

        /**
         * 时间格式判断
         */
//        String coatingDate = coatingDateDTO.getDate();
//        LocalDate parse = LocalDate.parse(coatingDate);

        Pattern pattern = Pattern.compile(Common.regex);
        if (!pattern.matcher(coatingDateDTO.getMakeDate()).matches()) {
            throw new ServiceException(ResponseCode.BAD_REQUEST, "日期格式错误,请提供纯数字格式!");
        }
        classDateDTOList.add(coatingDateDTO);
    }

    /**
     * 读完aop
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 获取数据
     */
    public List<ClassDataDTO> getClassDateDTOList(){
        return classDateDTOList;
    }
}
